package com.zhc.dejingapi.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhc.dejingapi.entity.QingFcmoroutingdetailEntity;
import com.zhc.dejingapi.entity.QingFcmoroutingprintEntity;
import com.zhc.dejingapi.mapper.QingFcmoroutingdetailMapper;
import com.zhc.dejingapi.mapper.QingFcmoroutingmainMapper;
import com.zhc.dejingapi.mapper.QingFcmoroutingprintMapper;
import com.zhc.dejingapi.service.QingFcmoroutingprintService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhc
 * @since 2024-09-03
 */
@Service
public class QingFcmoroutingprintServiceImpl extends ServiceImpl<QingFcmoroutingprintMapper, QingFcmoroutingprintEntity> implements QingFcmoroutingprintService {


    @Resource
    private QingFcmoroutingprintMapper qingFcmoroutingprintMapper;

    @Resource
    private QingFcmoroutingmainMapper qingFcmoroutingmainMapper;
    @Resource
    private QingFcmoroutingdetailMapper qingFcmoroutingdetailMapper;
    @Override
    public void printData(String strBody) {
        System.out.println(strBody);
        String now = DateUtil.now();
        LocalDateTime localDateTime = LocalDateTimeUtil.parse(now, DatePattern.NORM_DATETIME_PATTERN);
        if(strBody.contains("xm2")){
            String mainId=strBody.substring(strBody.indexOf("mainId")+7);
            QueryWrapper<QingFcmoroutingdetailEntity> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("main_id",mainId);
            List<QingFcmoroutingdetailEntity> qingFcmoroutingdetailEntityList = qingFcmoroutingdetailMapper.selectList(queryWrapper);
            for (QingFcmoroutingdetailEntity qfd:qingFcmoroutingdetailEntityList) {
                QingFcmoroutingprintEntity qingFcmoroutingprintEntity = new QingFcmoroutingprintEntity();
                qingFcmoroutingprintEntity.setDetailId(qfd.getDetailId());
                qingFcmoroutingprintEntity.setBarcodeStr(qfd.getBarcodeStr());
                qingFcmoroutingprintEntity.setIQty(qfd.getIQty());
                qingFcmoroutingprintEntity.setPrintTime(localDateTime);
                int insert = qingFcmoroutingprintMapper.insert(qingFcmoroutingprintEntity);
                if(insert>0){
                    Long printFrequency=qfd.getPrintFrequency();
                    if(printFrequency==null){
                        printFrequency=0L;
                    }
                    qfd.setPrintFrequency(printFrequency+1);
                    qfd.setLastPrintTime(localDateTime);
                    QueryWrapper<QingFcmoroutingdetailEntity> query=new QueryWrapper<>();
                    query.eq("detail_id",qfd.getDetailId());
                    qingFcmoroutingdetailMapper.update(qfd,query);
                }
            }
            }else if(strBody.contains("xm1")){
            String mainId=strBody.substring(strBody.indexOf("mainId")+7);
            QueryWrapper<QingFcmoroutingdetailEntity> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("detail_id",mainId);
            QingFcmoroutingdetailEntity qingFcmoroutingdetailEntity = qingFcmoroutingdetailMapper.selectOne(queryWrapper);
            Long printFrequency = qingFcmoroutingdetailEntity.getPrintFrequency();

            qingFcmoroutingdetailEntity.setPrintFrequency(printFrequency+1);
            qingFcmoroutingdetailEntity.setLastPrintTime(localDateTime);
            QueryWrapper<QingFcmoroutingdetailEntity> query=new QueryWrapper<>();
            query.eq("detail_id",qingFcmoroutingdetailEntity.getDetailId());
            qingFcmoroutingdetailMapper.update(qingFcmoroutingdetailEntity,query);


        }
    }
}
